﻿$(function() {
	usrAgnt = navigator.userAgent;

	var totalPanels	= $(".affiliates_scrollContainer").children().size();
	var movingDistance	= 616;
	var $panels = $('#affiliates_slider .affiliates_scrollContainer > div');
	var $container = $('#affiliates_slider .affiliates_scrollContainer');
	var curPanel = 0;
	var scroll = $('#affiliates_slider .affiliates_gallery_scroll').css('overflow', 'hidden');
	$panels.css({'float' : 'left','position' : 'relative'});
	$container.css({'width': (616* $panels.size()) +'px', 'left':'0px'});
	$("#affiliates_slider").data("currentlyMoving", false);
	$('#affiliates_arrow .left').css({'display':'none'});

	function change(direction, moveIndex) {
		if((direction && !(curPanel < totalPanels - 1)) || (!direction && (curPanel <= 0))) { return false; }
		if (($("#affiliates_slider").data("currentlyMoving") == false)) {
			$("#affiliates_slider").data("currentlyMoving", true);
			var next = direction ? curPanel + moveIndex : curPanel - moveIndex;
			var leftValue = $(".affiliates_scrollContainer").css("left");
			var movement = direction ? parseFloat(leftValue, 10) - (movingDistance * moveIndex) : parseFloat(leftValue, 10) + (movingDistance * moveIndex);
			var curThumb = $("#affiliates_thumbImg li.on");
			var removeClassEl = direction ? $(curThumb).next() : $(curThumb).prev();
			$(".affiliates_scrollContainer").stop().animate({"left": movement}, 300, function() {
				$("#affiliates_slider").data("currentlyMoving", false);
			});
			$(removeClassEl).addClass("on");
			$(curThumb).removeClass("on");
			curPanel = next;
			NonArr();
		}
	}
	
	$("#affiliates_thumbImg li").live("click", function(){
		if(!($(".affiliates_scrollContainer").is(":animated"))){
			var curElSize = $("#affiliates_thumbImg li.on").index();
			var clickElSize = $(this).index();
			if(curElSize !== clickElSize){
				if(curElSize > clickElSize){
					change(false, (curElSize-clickElSize));
				}
				else if(curElSize < clickElSize){
					change(true, (clickElSize-curElSize));
				}
				curPanel = clickElSize;
				$("#affiliates_thumbImg li").removeClass("on");
				$(this).addClass("on");
				NonArr();
			}
		}
	});
	$(".right").live("click", function(){
		change(true, 1);
	});
	$(".left").live("click", function(){
		change(false, 1);
	});
});

$(window).load(function () {
	if(usrAgnt.indexOf("Mobile") < 0){
		$('.greyScale').greyScale({ fadeTime: 100 });
	}
});
$(document).ready(function(){
	$(".affiliateCategory ul li").click(function(){
		$(".affiliateListTable").slideDown();
	});
	$(".btn_close").click(function(){
		$(".affiliateListTable").slideUp();
	});
});
function NonArr() {
	var indA = $('#affiliates_thumbImg .on').index();
	var indB = $('#affiliates_thumbImg ul li').size();
	if (indA == 0){
		$('#affiliates_arrow .left').css({'display':'none'});
		$('#affiliates_arrow .right').css({'display':'block'});
	} else if (indA == indB-1){
		$('#affiliates_arrow .left').css({'display':'block'});
		$('#affiliates_arrow .right').css({'display':'none'});
	} else {
		$('#affiliates_arrow .left').css({'display':'block'});
		$('#affiliates_arrow .right').css({'display':'block'});
	}
}